<template>
	<view class="register">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput v-model="name" type="text" placeholder="用户名"></wInput>
				<wInput v-model="phoneData" type="text" placeholder="邮箱"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="登录密码" isShowPass></wInput>
				<view class="verfiy">
					<input placeholder-style="color:#959595;" type="number" maxlength="4" placeholder="右方验证码"
						v-model="verimg" />
					<view class="verfiyCode">
						<image @click="refresh" :src="base64" mode=""></image>
					</view>
				</view>
				<wInput v-model="verCode" type="number" maxlength="6" placeholder="验证码" isShowCode ref="runCode"
					@setCode="getVerCode()"></wInput>

			</view>

			<wButton class="wbutton" text="注 册" :rotate="isRotate" @click.native="startReg()"></wButton>

			<!-- 底部信息 -->
			<view class="footer">
				<text @tap="isShowAgree" class="cuIcon" :class="showAgree?'cuIcon-radiobox':'cuIcon-round'"> 同意</text>
				<!-- 协议地址 -->
				<navigator url="" open-type="navigate">《协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import badgeMix from "../../mixin/tabbar-badge.js";
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '', // 用户/电话
				passData: '', //密码
				verCode: "", //验证码
				showAgree: true, //协议是否选择
				isRotate: false, //是否加载旋转
				base64: '',
				verimg: '',
				name: ''
			}
		},
		components: {
			wInput,
			wButton,
		},
		mixins: [badgeMix],
		mounted() {
			_this = this;
			// this.a()
		},
		onLoad() {
			this.getverf()
		},
		methods: {
			getverf() {
				uni.request({
					url: 'http://127.0.0.1:8889/user-server/api/user/v1/verityCode',
					responseType: 'arraybuffer',
					method: 'GET',
					// responseType: 'blob',
					success(res) {
						const arrayBuffer = new Uint8Array(res.data)
						// let base = 'data:image/png;base64,' + btoa(new Uint8Array(res.data).reduce((data,
						// 	byte) => data + String.fromCharCode(byte), ''))
						const base = `data:image/png;base64,${uni.arrayBufferToBase64(arrayBuffer)}`


						_this.base64 = base
					}
				})
			},
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			refresh() {
				this.getverf()
			},
			getVerCode() {
				// 获取验证码
				let aqq = /^[1-9][0-9]{4,10}@qq.com$/;
				if (!aqq.test(_this.phoneData)) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '邮箱不正确'
					});
					return false;
				}
				if (_this.verimg.length < 4) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '图像验证码不正确'
					});
					return false;
				} else {
					uni.$http.get(
							`/user-server/api/user/v1/sendRegisterCode?to=${_this.phoneData}&captcha=${_this.verimg}`)
						.then(res => {
							if (res.data.code === 200) {
								// console.log(res)
								this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
							}
						})
				}

				// console.log("获取验证码")

				// uni.showToast({
				// 	icon: 'none',
				// 	position: 'bottom',
				// 	title: '模拟倒计时触发'
				// });

				// setTimeout(function() {
				// 	_this.$refs.runCode.$emit('runCode', 0); //假装模拟下需要 终止倒计时
				// 	uni.showToast({
				// 		icon: 'none',
				// 		position: 'bottom',
				// 		title: '模拟倒计时终止'
				// 	});
				// }, 3000)
			},
			startReg() {
				//注册
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (!this.name) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请先同意《协议》'
					});
					return false;
				}
				let aqq = /^[1-9][0-9]{4,10}@qq.com$/;
				if (!aqq.test(_this.phoneData)) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '邮箱不正确'
					});
					return false;
				}
				if (_this.passData.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return false;
				}
				if (_this.verimg.length < 4) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '图片验证码不正确'
					});
					return false;
				} else {
					_this.isRotate = true
					uni.$http.post(`/user-server/api/user/v1/register`, {
						"code": this.verCode,
						// "head_img": "{{$randomImageUrl}}",
						"mail": this.phoneData,
						"name": this.name,
						"pwd": this.passData,
						"sex": 1,
						// "slogan": "{{$randomCatchPhrase}}"
					}).then(res => {
						if (res.data.code === 200) {
							_this.isRotate = false
						}
						// console.log(res)
					})
				}


			}
		}
	}
</script>

<style scoped>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");

	* {
		box-sizing: border-box;
	}

	.verfiy {
		width: 100%;
		height: 100rpx;
		/* background-color: #007AFF; */
		display: flex;
	}

	.verfiy input {
		width: 50%;
		height: 100%;
		/* padding-left: 20rpx; */
		text-align: center;
		border-radius: 60rpx;
		box-shadow: 1px 1px 6px 3px rgba(179, 179, 179, 0.2);
		/* background-color: #ff557f; */
	}

	.verfiyCode {
		width: 40%;
		margin-left: 40rpx;
		height: 100rpx;
		border-radius: 60rpx;
		box-shadow: 1px 1px 6px 3px rgba(179, 179, 179, 0.2);
		overflow: hidden;
	}

	.verfiy image {
		width: 100%;
		height: 100%;
		border-radius: 60rpx;
		box-shadow: 1px 1px 6px 3px rgba(179, 179, 179, 0.2);
		/* background-color: #07C160; */
	}
</style>
